<template>
  <div>
    <TodoHeader @add-todo="addFn" />
    <TodoBody :list="list" @todo-del="delTodoFn" />
    <TodoFooter
      :list="list"
      @clear-Todo="clearDoneTodoFn"
      v-if="list.length > 0"
    />
  </div>
</template>

<script>
import "./assets/css/base.css";
import "./assets/css/index.css";
import TodoHeader from "./components/TodoHeader.vue";
import TodoBody from "./components/TodoBody.vue";
import TodoFooter from "./components/TodoFooter.vue";
export default {
  components: {
    TodoHeader,
    TodoBody,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(window.localStorage.getItem("todo")) || [
        { id: 100, name: "吃饭", isDone: true },
        { id: 201, name: "睡觉", isDone: false },
        { id: 103, name: "打豆豆", isDone: true },
      ],
    };
  },
  methods: {
    addFn(name) {
      const importition = {
        id: Date.now(),
        name,
        isDone: false,
      };
      this.list.push(importition);
    },
    delTodoFn(index) {
      this.list.splice(index, 1);
    },
    clearDoneTodoFn() {
      this.list = this.list.filter((item) => item.isDone === false);
    },
  },
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        window.localStorage.setItem("todo", JSON.stringify(newVal));
      },
    },
  },
};
</script>

<style>
</style>